package com.parenting.sevice.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;

import com.parenting.common.Constant;
import com.parenting.entities.Shopper;
import com.parenting.entities.ShopperChild;
import com.parenting.enums.ChildLabelEnum;
import com.parenting.mapper.ShopperChildMapper;
import com.parenting.properties.BlogProperties;
import com.parenting.properties.ImWechatProperties;
import com.parenting.request.ShopperChildCheck;
import com.parenting.response.ShopperChildResponse;
import com.parenting.sevice.ShopperChildService;
import com.parenting.sevice.ShopperService;
import com.parenting.util.IMUtil;
import com.parenting.util.TLSSigAPIv2;
import com.parenting.util.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 宝宝认证 服务实现类
 * </p>
 *
 * @author zcj
 * @since 2021-05-12
 */
@Service
public class ShopperChildServiceImpl extends ServiceImpl<ShopperChildMapper, ShopperChild> implements ShopperChildService {

    @Autowired
    private ShopperService shopperService;


    @Override
    public Result check(ShopperChildCheck childDTO, Long id) throws JsonProcessingException {
        ShopperChild shopperChild = getById(id);
        BeanUtil.copyProperties(childDTO, shopperChild);
        updateById(shopperChild);
        Shopper shopper = shopperService.getById(shopperChild.getShopperId());
        // 通过
        if (childDTO.getStatus().intValue() == Constant.CER_STATUS_THREE) {
            int count = count(query().eq("SHOPPER_ID", id));
            // 更改标签
            shopper.setFamilyStatus(ChildLabelEnum.getChinese(count + 1));
            // 修改个人信息为认证
            shopper.setCertStatus(Constant.CER_STATUS_SUCCESS);
            // 查询有几个宝宝
            shopperService.updateById(shopper);
            sendIM(shopper.getAccountNum().toString(), "您好，您提交的“宝宝认证”已通过认证，恭喜您哦~");
        } else {
            sendIM(shopper.getAccountNum().toString(), "您好，您提交的“宝宝认证”未通过认证，请按要求提交资料哦~");
        }
        return Result.success();
    }



    @Override
    public Result<Page<ShopperChildResponse>> childCheckList(Integer current, Integer size, Integer type) {
        Page<ShopperChildResponse> page = new Page<>(current, size);
        Page<ShopperChildResponse> pageList = baseMapper.childCheckList(page, type);
        return Result.ok(pageList);
    }




    @Override
    public Result<ShopperChildResponse> childInfo(Long id) {
        ShopperChild shopperChild = getById(id);
        return Result.ok(BeanUtil.copyProperties(shopperChild, ShopperChildResponse.class));
    }

    @Autowired
    private BlogProperties blogProperties;

    private JSONObject sendIM(String account, String context) throws JsonProcessingException {
        ImWechatProperties wechatIm = blogProperties.getWechatIm();
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(wechatIm.getSdkAppId(), wechatIm.getKey());
        String userSig = tlsSigAPIv2.genUserSig("adminuser", wechatIm.getExpire());
        String url1 = StrUtil.format(Constant.IM_URL, wechatIm.getSdkAppId(), userSig, System.currentTimeMillis());
        return IMUtil.senMsg(url1, account, context);
    }
}
